package com.devicemantra.coordinate.ds.tests;
import com.devicemantra.coordinate.dataobjects.*;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.*;

import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;

import com.devicemantra.coordinate.dataobjects.CDepartmentFactory;
import com.devicemantra.coordinate.exceptions.operations.CDepartmentAlreadyExistsException;
import com.google.appengine.api.datastore.Blob;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.oreilly.servlet.*;
import com.oreilly.servlet.multipart.*;

/* Testing O'Reily Multipart form-data upload */

public class TestMultiUpload extends HttpServlet 
{
	private ContactInfo mContactInfo = null;
	private String mDeptName = null;
	private String  mDeptDesc = null;
	private String mDeptContName = null;
	private String mDeptContMob = null;
	private String mDeptContEmail = null;
	private String mDeptDesg = null;
	private String mDeptPassPortNum = null;
	
	public void doPost(HttpServletRequest req, HttpServletResponse res)
			throws ServletException,IOException 
	{
		
		/* First let us create a parser for the incoming multipart form-data */
		MultipartParser lMultipartParser = new MultipartParser(req,1000000);
		Logo lLogo = null;
		while( true )
		{
			Part lPart = lMultipartParser.readNextPart();
			if( lPart == null )
				break;
			
			/* Check whether this Part is a file or a form field ? */
			if( lPart.isParam() )
			{
				ParamPart lParamPart = (ParamPart ) lPart;
				String lName = lPart.getName();
				String lValue = lParamPart.getStringValue();
				/* Process the parameter */
				updateParameter(lName,lValue);
			}
			else 
			{
				/* It's a file , process the file */
				FilePart lFilePart = (FilePart)lPart;
				System.out.println("File  name is " + lPart.getName() + " Value is " + lFilePart.getFileName());
				InputStream lImgStream = lFilePart.getInputStream();
			    Blob lIimageBlob = new Blob(IOUtils.toByteArray(lImgStream));
			    lLogo = new Logo(lFilePart.getFileName(),lFilePart.getContentType() ,lIimageBlob);
			    
			}
		}
		mContactInfo = new ContactInfo(mDeptContName, mDeptDesg, mDeptContEmail, mDeptContMob, mDeptPassPortNum);
		try {
			CDepartmentFactory.createNewDepartment(mDeptName, mDeptDesc, lLogo, mContactInfo);
		} catch (CDepartmentAlreadyExistsException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    String lMessage="Department added ";
	    res.getOutputStream().print("<script language=\"javascript\" type=\"text/javascript\">window.parent.test_parent(" + "\"<B> <font color=green>" + lMessage +   "</font></B>\""  +  ")</script>"); 
	}	
	
	public void updateParameter(String pName, String pValue)
	{
		if( pName.equals("dept_name"))
		{
			setDeptName(pValue);
			System.out.println("Department dept_name set to " + mDeptName);
		}
		
		if( pName.equals("dept_desc"))
		{
			setDeptDesc(pValue);
			System.out.println("Department dept_desc set to " + mDeptDesc);
		}
		
		if( pName.equals("dept_cont_name"))
		{
			setDeptContName(pValue);
			System.out.println("Department dept_cont_name set to " + mDeptContName);
		}
		
		if( pName.equals("dept_cont_mob"))
		{
			setDeptContMob(pValue);
			System.out.println("Department dept_cont_mob set to " + mDeptContMob);
		}
		
		if( pName.equals("dept_cont_email"))
		{
			setDeptContEmail(pValue);
			System.out.println("Department dept_cont_email set to " + mDeptContEmail);
		}
		
		if( pName.equals("dept_cont_desg"))
		{
			setDeptContDesg(pValue);
			System.out.println("Department dept_cont_desg set to " + mDeptDesg);
		}
		
		if( pName.equals("dept_cont_pp"))
		{
			setDeptContPassPortNumber(pValue);
			System.out.println("Department dept_cont_pp set to " + mDeptPassPortNum);
		}
	}

	
	public void setDeptName(String mDeptName) {
		this.mDeptName = mDeptName;
	}

	public void setDeptDesc(String mDeptDesc) {
		this.mDeptDesc = mDeptDesc;
	}

	public void setDeptContName(String mDeptContName) {
		this.mDeptContName = mDeptContName;
	}

	public void setDeptContMob(String mDeptContMob) {
		this.mDeptContMob = mDeptContMob;
	}

	public void setDeptContEmail(String pDeptContEmail) {
		this.mDeptContEmail = pDeptContEmail;
	}
	
	public void setDeptContDesg(String pDeptDesg) 
	{
		mDeptDesg = pDeptDesg;
	}

	public void setDeptContPassPortNumber(String pPassPortNum) {
		this.mDeptPassPortNum = pPassPortNum;
	}
	
	
	
	
	
	
}
